package o;

import android.os.Parcel;
import android.text.TextUtils;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import com.huawei.appgallery.parcelable.AutoParcelable;
import com.huawei.appgallery.parcelable.CreateExecption;
import com.huawei.appgallery.parcelable.EnableAutoParcel;
import com.huawei.appgallery.parcelable.impl.ParcelableArrayTypeProcess;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public final class cqg {
    private static final Map<cqc, cqj> ॱ = new HashMap();

    static {
        ॱ.put(cqc.Binder, new cqi());
        ॱ.put(cqc.Boolean, new cqk());
        ॱ.put(cqc.Bundle, new cqm());
        ॱ.put(cqc.ByteArray, new cqn());
        ॱ.put(cqc.Double, new cqo());
        ॱ.put(cqc.Float, new cql());
        ॱ.put(cqc.HashMap, new cqp());
        ॱ.put(cqc.IntArray, new cqq());
        ॱ.put(cqc.Integer, new cqs());
        ॱ.put(cqc.Interface, new cqr());
        ॱ.put(cqc.List, new cqt());
        ॱ.put(cqc.Long, new cqx());
        ॱ.put(cqc.ParcelableArray, new ParcelableArrayTypeProcess());
        ॱ.put(cqc.Parcelable, new cqv());
        ॱ.put(cqc.StringArray, new cqy());
        ॱ.put(cqc.StringList, new cqu());
        ॱ.put(cqc.ᐝॱ, new cqw());
    }

    public static <T extends AutoParcelable> T ˋ(Class<T> cls, Parcel parcel) {
        try {
            Constructor declaredConstructor = cls.getDeclaredConstructor(new Class[0]);
            boolean isAccessible = declaredConstructor.isAccessible();
            declaredConstructor.setAccessible(true);
            try {
                AutoParcelable autoParcelable = (AutoParcelable) declaredConstructor.newInstance(new Object[0]);
                try {
                    ॱ(autoParcelable, parcel);
                } catch (Throwable e) {
                    cqz.ˏ.ॱ("SafeParcel", "can not readObject", e);
                }
                declaredConstructor.setAccessible(isAccessible);
                return autoParcelable;
            } catch (IllegalAccessException e2) {
                throw new CreateExecption("newInstance failed");
            } catch (InstantiationException e3) {
                throw new CreateExecption("newInstance failed");
            } catch (InvocationTargetException e4) {
                throw new CreateExecption("newInstance failed");
            } catch (Throwable th) {
                declaredConstructor.setAccessible(isAccessible);
            }
        } catch (NoSuchMethodException e5) {
            throw new CreateExecption("createObject() requires a default constructor");
        } catch (SecurityException e6) {
            throw new CreateExecption("createObject() requires a public constructor");
        }
    }

    public static void ˎ(AutoParcelable autoParcelable, Parcel parcel, int i) {
        Class cls = autoParcelable.getClass();
        int ॱ = cqh.ॱ(parcel, 20293);
        ॱ(autoParcelable, parcel, cls);
        for (Class cls2 = cls; cls2 != null; cls2 = cls2.getSuperclass()) {
            for (Field field : cls2.getDeclaredFields()) {
                if (field.isAnnotationPresent(EnableAutoParcel.class)) {
                    try {
                        ˋ(autoParcelable, parcel, field, i);
                    } catch (Exception e) {
                        cqz.ˏ.ˎ("SafeParcel", "Error writing field: " + e);
                    }
                }
            }
        }
        cqh.ˏ(parcel, ॱ);
    }

    public static ClassLoader ˏ(Class cls) {
        return cls == null ? ClassLoader.getSystemClassLoader() : cls.getClassLoader();
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static void ॱ(com.huawei.appgallery.parcelable.AutoParcelable r13, android.os.Parcel r14, java.lang.Class r15) {
        /*
        r0 = r13.getClass();
        r1 = r0.getTypeParameters();
        r4 = new java.util.ArrayList;
        r4.<init>();
        r2 = new java.util.ArrayList;
        r2.<init>();
        r3 = r1.length;
        r0 = 0;
    L_0x0014:
        if (r0 >= r3) goto L_0x002c;
    L_0x0016:
        r5 = r1[r0];
        r6 = r5.getName();
        r6 = android.text.TextUtils.isEmpty(r6);
        if (r6 != 0) goto L_0x0029;
    L_0x0022:
        r5 = r5.getName();
        r2.add(r5);
    L_0x0029:
        r0 = r0 + 1;
        goto L_0x0014;
    L_0x002c:
        r0 = r2.isEmpty();
        if (r0 != 0) goto L_0x00b0;
    L_0x0032:
        r3 = r15.getDeclaredFields();
        r5 = r3.length;
        r0 = 0;
    L_0x0038:
        if (r0 >= r5) goto L_0x00b0;
    L_0x003a:
        r6 = r3[r0];
        r7 = com.huawei.appgallery.parcelable.EnableAutoParcel.class;
        r7 = r6.isAnnotationPresent(r7);
        if (r7 == 0) goto L_0x0089;
    L_0x0044:
        r7 = r6.getGenericType();
        r7 = r7.toString();
        r7 = r2.contains(r7);
        if (r7 == 0) goto L_0x0089;
    L_0x0052:
        r7 = r6.isAccessible();
        r8 = 1;
        r6.setAccessible(r8);
        r8 = r6.getName();
        r9 = r6.get(r13);	 Catch:{ IllegalAccessException -> 0x008c }
        if (r9 == 0) goto L_0x0086;
    L_0x0064:
        r9 = r9.getClass();	 Catch:{ IllegalAccessException -> 0x008c }
        r9 = r9.getCanonicalName();	 Catch:{ IllegalAccessException -> 0x008c }
        r10 = new java.lang.StringBuilder;	 Catch:{ IllegalAccessException -> 0x008c }
        r10.<init>();	 Catch:{ IllegalAccessException -> 0x008c }
        r10 = r10.append(r8);	 Catch:{ IllegalAccessException -> 0x008c }
        r11 = "|";
        r10 = r10.append(r11);	 Catch:{ IllegalAccessException -> 0x008c }
        r9 = r10.append(r9);	 Catch:{ IllegalAccessException -> 0x008c }
        r9 = r9.toString();	 Catch:{ IllegalAccessException -> 0x008c }
        r4.add(r9);	 Catch:{ IllegalAccessException -> 0x008c }
    L_0x0086:
        r6.setAccessible(r7);
    L_0x0089:
        r0 = r0 + 1;
        goto L_0x0038;
    L_0x008c:
        r9 = move-exception;
        r9 = o.cqz.ˏ;	 Catch:{ all -> 0x00ab }
        r10 = "SafeParcel";
        r11 = new java.lang.StringBuilder;	 Catch:{ all -> 0x00ab }
        r11.<init>();	 Catch:{ all -> 0x00ab }
        r12 = "can not get the value of the field:";
        r11 = r11.append(r12);	 Catch:{ all -> 0x00ab }
        r8 = r11.append(r8);	 Catch:{ all -> 0x00ab }
        r8 = r8.toString();	 Catch:{ all -> 0x00ab }
        r9.ॱ(r10, r8);	 Catch:{ all -> 0x00ab }
        r6.setAccessible(r7);
        goto L_0x0089;
    L_0x00ab:
        r0 = move-exception;
        r6.setAccessible(r7);
        throw r0;
    L_0x00b0:
        r0 = new o.cqy;
        r0.<init>();
        r2 = 0;
        r3 = 0;
        r1 = r1.length;
        r1 = new java.lang.String[r1];
        r4 = r4.toArray(r1);
        r4 = (java.lang.String[]) r4;
        r5 = 0;
        r6 = 0;
        r1 = r14;
        r0.ˊ(r1, r2, r3, r4, r5, r6);
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.cqg.ॱ(com.huawei.appgallery.parcelable.AutoParcelable, android.os.Parcel, java.lang.Class):void");
    }

    private static void ॱ(@NonNull AutoParcelable autoParcelable, Parcel parcel) {
        Class cls = autoParcelable.getClass();
        SparseArray sparseArray = new SparseArray();
        for (Class cls2 = cls; cls2 != null; cls2 = cls2.getSuperclass()) {
            int readInt;
            for (Field field : cls2.getDeclaredFields()) {
                if (field.isAnnotationPresent(EnableAutoParcel.class)) {
                    int value = ((EnableAutoParcel) field.getAnnotation(EnableAutoParcel.class)).value();
                    if (sparseArray.get(value) != null) {
                        throw new CreateExecption("Field number " + value + " is used twice in " + cls2.getName() + " for fields " + field.getName() + " and " + ((Field) sparseArray.get(value)).getName());
                    }
                    sparseArray.put(value, field);
                }
            }
        }
        Class cls3 = autoParcelable.getClass();
        int ˋ = cqf.ˋ(parcel);
        Map ˋ2 = ˋ(parcel);
        while (parcel.dataPosition() < ˋ) {
            readInt = parcel.readInt();
            int ˏ = cqf.ˏ(readInt);
            Field field2 = (Field) sparseArray.get(ˏ);
            if (field2 == null) {
                cqz.ˏ.ˏ("SafeParcel", "Unknown field num " + ˏ + " in " + cls3.getName() + ", skipping.");
                cqf.ˏ(parcel, readInt);
            } else {
                try {
                    ˎ(autoParcelable, parcel, field2, readInt, ˋ2);
                } catch (a e) {
                    cqf.ॱ(parcel, readInt);
                    cqz.ˏ.ˎ("SafeParcel", "Error reading field: " + ˏ + " in " + cls3.getName() + ", skipping." + e.getMessage());
                    cqf.ˏ(parcel, readInt);
                }
            }
        }
        if (parcel.dataPosition() > ˋ) {
            throw new CreateExecption("Overread allowed size end=" + ˋ + "-" + parcel.dataPosition());
        }
    }

    private static Map<String, String> ˋ(Parcel parcel) {
        Map<String, String> hashMap = new HashMap();
        String[] ॱ = cqy.ॱ(parcel, parcel.readInt());
        if (ॱ != null) {
            for (Object obj : ॱ) {
                if (!TextUtils.isEmpty(obj)) {
                    String[] split = obj.split("\\|");
                    if (split.length == 2) {
                        hashMap.put(split[0], split[1]);
                    }
                }
            }
        }
        return hashMap;
    }

    private static void ˋ(AutoParcelable autoParcelable, Parcel parcel, Field field, int i) throws IllegalAccessException {
        int value = ((EnableAutoParcel) field.getAnnotation(EnableAutoParcel.class)).value();
        boolean mayNull = ((EnableAutoParcel) field.getAnnotation(EnableAutoParcel.class)).mayNull();
        boolean isAccessible = field.isAccessible();
        field.setAccessible(true);
        cqj o_cqj = (cqj) ॱ.get(cqc.ˏ(field));
        if (o_cqj == null) {
            cqz.ˏ.ॱ("SafeParcel", "can not find process to write:" + field.getName());
        } else {
            o_cqj.ˊ(parcel, field, value, field.get(autoParcelable), i, mayNull);
        }
        field.setAccessible(isAccessible);
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static void ˎ(com.huawei.appgallery.parcelable.AutoParcelable r7, android.os.Parcel r8, java.lang.reflect.Field r9, int r10, java.util.Map<java.lang.String, java.lang.String> r11) {
        /*
        r6 = r9.isAccessible();
        r0 = 1;
        r9.setAccessible(r0);
        r0 = o.cqc.ˏ(r9);
        r1 = ॱ;	 Catch:{ IllegalAccessException -> 0x0041 }
        r0 = r1.get(r0);	 Catch:{ IllegalAccessException -> 0x0041 }
        r0 = (o.cqj) r0;	 Catch:{ IllegalAccessException -> 0x0041 }
        if (r0 != 0) goto L_0x0038;
    L_0x0016:
        r0 = o.cqz.ˏ;	 Catch:{ IllegalAccessException -> 0x0041 }
        r1 = "SafeParcel";
        r2 = new java.lang.StringBuilder;	 Catch:{ IllegalAccessException -> 0x0041 }
        r2.<init>();	 Catch:{ IllegalAccessException -> 0x0041 }
        r3 = "can not find process to read:";
        r2 = r2.append(r3);	 Catch:{ IllegalAccessException -> 0x0041 }
        r3 = r9.getName();	 Catch:{ IllegalAccessException -> 0x0041 }
        r2 = r2.append(r3);	 Catch:{ IllegalAccessException -> 0x0041 }
        r2 = r2.toString();	 Catch:{ IllegalAccessException -> 0x0041 }
        r0.ॱ(r1, r2);	 Catch:{ IllegalAccessException -> 0x0041 }
    L_0x0034:
        r9.setAccessible(r6);
    L_0x0037:
        return;
    L_0x0038:
        r1 = r7;
        r2 = r9;
        r3 = r8;
        r4 = r10;
        r5 = r11;
        r0.ˋ(r1, r2, r3, r4, r5);	 Catch:{ IllegalAccessException -> 0x0041 }
        goto L_0x0034;
    L_0x0041:
        r0 = move-exception;
        r0 = o.cqz.ˏ;	 Catch:{ all -> 0x004f }
        r1 = "SafeParcel";
        r2 = "can not set field value";
        r0.ˎ(r1, r2);	 Catch:{ all -> 0x004f }
        r9.setAccessible(r6);
        goto L_0x0037;
    L_0x004f:
        r0 = move-exception;
        r9.setAccessible(r6);
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.cqg.ˎ(com.huawei.appgallery.parcelable.AutoParcelable, android.os.Parcel, java.lang.reflect.Field, int, java.util.Map):void");
    }
}
